package com.jp.fm.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jp.fm.data.UserAccount;

public class Login extends ServletParent {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public Login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public final void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public final void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {

		doPost(request, response);
	}

	public final void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {

		String username = request.getParameter("username");
		String password = request.getParameter("password");

		UserAccount uAcct = UserAccount.findByName(username);
		if (null != uAcct && uAcct.getPassword().equals(password)) {
			request.getSession().setAttribute("UserAccount", uAcct);
			RequestDispatcher rd = request
					.getRequestDispatcher("/secured/ShowAccountSummary");
			rd.forward(request, response);
		} else {
			request.setAttribute("error", "Unknown login, try again");
			RequestDispatcher rd = request.getRequestDispatcher("/Login.jsp");
			rd.forward(request, response);
		}
	}
}